package com.smartHire.controller.User.Cart;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.smartHire.domain.Cart;
import com.smartHire.service.CartService;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.Map;

@WebServlet(name = "UpdateCartServlet", value = "/UpdateCartServlet")
public class UpdateCartServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String BufferArray= request.getParameter("BufferArray");
        System.out.println(BufferArray);

        WebApplicationContext ctx = null;
        //使用框架中的方法，获取容器对象
        ServletContext sc = getServletContext();
        ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);

        try {
            CartService cartService = (CartService) ctx.getBean("cartService");

            JSONArray array = JSONObject.parseArray(BufferArray);
            for (Object a: array)
            {
                Map map = (Map) a;

                Cart cart = new Cart();
                cart.setC_id(Integer.parseInt(map.get("Goods_id").toString()));
                cart.setNumber(Integer.parseInt(map.get("number").toString()));
                cart.setDay(Integer.parseInt(map.get("time").toString()));

                if(cartService.updateCart(cart)==1)
                {
                    response.getWriter().write("OK");
                }else
                {
                    response.getWriter().write("NO");
                }


            }



        }catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
